package Input
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.Dictionary;
	
    public class GameInput 
	{
        public static var i:GameInput;

        private var mouseHitStatus:Boolean = false;
        private var keyDownStatus:Dictionary;
        public var mousex:int;
        public var mousey:int;
		private var mouseWheelStatus:Boolean=false;
        private var keysHitStatus:Dictionary;
		private var delta:int=0;
		private var mouseMoveStatus:Boolean=false;
		private var stage:Stage;
		private var mouseDownStatus:Boolean = false;

        public function GameInput(_stage:Stage)
		{
			stage=_stage;
            keyDownStatus = new Dictionary();
            keysHitStatus = new Dictionary();
            super();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
            i = this;
        }
		public function keyDown(keyCode:int):Boolean
		{
			return keyDownStatus[keyCode];
		}
		public function keyHit(keyCode:int):Boolean
		{
			var  _keysHitStatus:Boolean=false;
			if(keyDown(keyCode))
			{
				if(!keysHitStatus[keyCode])
				{
					keysHitStatus[keyCode]=true;
					_keysHitStatus=true;
				}
			}
			return _keysHitStatus;
		}
		public function get mHit():Boolean
		{
			var _mouseHitStatus:Boolean = mouseHitStatus;
			mouseHitStatus = false;
			return _mouseHitStatus;
		}
		public function get mMove():Boolean
		{
			var _mouseMoveStatus:Boolean = mouseMoveStatus;
			if(!mDown)
			{
				mouseMoveStatus = false;
			}
			return _mouseMoveStatus;
		}
		public function get mDown():Boolean
		{
			return mouseDownStatus;
		}
		public function get wheelDelta():int
		{
			var _delta:int=delta;
			delta=0;
			return _delta;
		}
		public function get mWheel():Boolean
		{
			var _mouseWheelStatus:Boolean=mouseWheelStatus;
			mouseWheelStatus=false;
			return _mouseWheelStatus;
		}
		private function mouseWheelHandler(event:MouseEvent):void
		{
			delta=event.delta;
			mouseWheelStatus=true;
		}
		private function mouseUpHandler(event:MouseEvent):void
		{
            mouseDownStatus = false;
        }
		private function keyUpHandler(event:KeyboardEvent):void
		{
			keysHitStatus[event.keyCode]=false;
            keyDownStatus[event.keyCode] = false;
        }
		private function mouseMoveHandler(event:MouseEvent):void
		{
            mousex = event.stageX;
            mousey = event.stageY;
			
			mouseMoveStatus=true;
        }
		private function mouseDownHandler(event:MouseEvent):void
		{
			mouseDownStatus = true;
        }
		private function mouseClickHandler(event:MouseEvent):void
		{
			mouseHitStatus = true;
		}
		private function keyDownHandler(event:KeyboardEvent):void
		{
			if(stage.focus is TextField)
			{
				var field:TextField=stage.focus as TextField;
				if(field.selectable)
				{
					return;
				}
				if(field.type==TextFieldType.INPUT)
				{
					return;
				}
			}
            keyDownStatus[event.keyCode] = true;
        }
    }
}